home *** CD-ROM | disk | FTP | other *** search
/ Network CD 2 / Network CD - Volume 2.iso / programs / internet / tcp / amitcp / amitcp-api-22.lha / AmiTCP-2.2 / src / netlib / autoinitd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-18  |  4.1 KB  |  157 lines

  1. RCS_ID_C="$Id: autoinitd.c,v 1.6 1993/10/18 05:59:52 jraja Exp $";
  2. /*
  3.  * SAS C Autoinitialization Functions for Daemons
  4.  *
  5.  * Author: ppessi <Pekka.Pessi@hut.fi>
  6.  *
  7.  * Copyright © 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>
  8.  *                  Helsinki University of Technology, Finland.
  9.  *                  All rights reserved.
  10.  *
  11.  * Created      : Mon May 24 19:30:06 1993 ppessi
  12.  * Last modified: Mon Oct 18 07:59:28 1993 jraja
  13.  */
  14.  
  15. /****** inetdlib.doc/autoinitd **********************************************
  16. *
  17. *   NAME
  18. *       autoinitd - SAS C Autoinitialization Functions for Daemons
  19. *
  20. *   SYNOPSIS
  21. *       void _STIopenSockets(void);
  22. *       void _STDcloseSockets(void);
  23. *       long server_socket;
  24. *
  25. *   DESCRIPTION
  26. *       These are SASC autoinitialization functions for internet daemons
  27. *       started by inetd, Internet super-server. Upon startup, the server
  28. *       socket is obtained with ObtainSocket() library call. If successful,
  29. *       the socket id is stored to the global variable server_socket. If the
  30. *       socket is not obtainable, the server_socket contains value -1.
  31. *       If the server_socket is not valid, the server may try to accept() a
  32. *       new connection and act as a stand-alone server.
  33. *
  34. *   RESULT
  35. *       server_socket - positive socket id for success or -1 for failure.
  36. *
  37. *   NOTES
  38. *       _STIopenSockets() also checks that the system version is at
  39. *       least 37. It puts up a requester if the bsdsocket.library
  40. *       is not found or is of wrong version.
  41. *
  42. *       The autoinitialization and autotermination functions are
  43. *       features specific to the SAS C6. However, these functions
  44. *       can be used with other (ANSI) C compilers, too. Example
  45. *       follows: 
  46. *
  47. *       \* at start of main() *\
  48. *
  49. *       atexit(_STDcloseSockets);
  50. *       _STDopenSockets();
  51. *
  52. *   AUTHOR
  53. *       Jarno Rajahalme, Pekka Pessi, 
  54. *       the AmiTCP/IP Group <amitcp-group@hut.fi>,
  55. *       Helsinki University of Technology, Finland.
  56. *
  57. *   SEE ALSO
  58. *       serveraccept(), netutil/inetd
  59. *
  60. *****************************************************************************
  61. *
  62. */
  63.  
  64. #include <exec/types.h>
  65. #include <exec/libraries.h>
  66. #include <dos/dosextens.h>
  67.  
  68. #include <intuition/intuition.h>
  69.  
  70. #include <proto/socket.h>
  71. #include <proto/exec.h>
  72. #include <proto/intuition.h>
  73.  
  74. #include <stdlib.h>
  75.  
  76. #include <inetd.h>
  77.  
  78. struct Library *SocketBase = 0L;
  79.  
  80. int errno = 0;
  81.  
  82. long server_socket = -1;
  83.  
  84. #ifndef SOCKETNAME
  85. #define SOCKETNAME "bsdsocket.library"
  86. #endif
  87.  
  88. #define SOCKETVERSION 2        /* minimum bsdsocket version to use */
  89.  
  90. extern STRPTR _ProgramName;    /* SAS startup module defines this :-) */
  91.  
  92. void __stdargs
  93. _STIopenSockets(void)
  94. {
  95.   struct Process *me = (struct Process *)FindTask(0L);
  96.   struct DaemonMessage *dm = (struct DaemonMessage *)me->pr_ExitData;
  97.   struct Library *IntuitionBase;
  98.   STRPTR errorStr;
  99.  
  100.   /*
  101.    * Check OS version
  102.    */
  103.   if ((*(struct Library **)4)->lib_Version < 37)
  104.     exit(20);
  105.  
  106.   /*
  107.    * Open bsdsocket.library
  108.    */
  109.   if ((SocketBase = OpenLibrary(SOCKETNAME, SOCKETVERSION)) != NULL) {
  110.     /*
  111.      * Succesfull. Now tell bsdsocket.library the address of our errno
  112.      */
  113.     SetErrnoPtr(&errno, sizeof(errno));
  114.     
  115.     if (dm && server_socket == -1) {
  116.       server_socket = ObtainSocket(dm->dm_Id, dm->dm_Family, dm->dm_Type, 0);
  117.       if (server_socket == -1)
  118.     exit(DERR_OBTAIN);
  119.       if (server_socket != 0) 
  120.     Dup2Socket(server_socket, 0);
  121.       if (server_socket != 1) 
  122.     Dup2Socket(server_socket, 1);
  123.       if (server_socket != 2) 
  124.     Dup2Socket(server_socket, 2);
  125.     }
  126.     return;
  127.   }
  128.   else
  129.     errorStr = "AmiTCP/IP version 2 or later must be started first.";
  130.   
  131.   IntuitionBase = OpenLibrary("intuition.library", 36);
  132.  
  133.   if (IntuitionBase != NULL) {
  134.     struct EasyStruct libraryES;
  135.     
  136.     libraryES.es_StructSize = sizeof(libraryES);
  137.     libraryES.es_Flags = 0;
  138.     libraryES.es_Title = _ProgramName;
  139.     libraryES.es_TextFormat = errorStr;
  140.     libraryES.es_GadgetFormat = "Exit %s";
  141.  
  142.     EasyRequestArgs(NULL, &libraryES, NULL, (APTR)&_ProgramName);
  143.  
  144.     CloseLibrary(IntuitionBase);
  145.   }
  146.   exit(DERR_LIB);
  147. }
  148.  
  149. void __stdargs
  150. _STDcloseSockets(void)
  151. {
  152.   if (SocketBase) {
  153.     CloseLibrary(SocketBase);
  154.     SocketBase = NULL;
  155.   }
  156. }
  157.